package com.example.easydownloadback.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.easydownloadback.domin.FileAndType;
import com.example.easydownloadback.domin.FileType;
import com.example.easydownloadback.mapper.FileAndTypeMapper;
import com.example.easydownloadback.service.FileAndTypeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FileAndTypeServiceImpl extends ServiceImpl<FileAndTypeMapper, FileAndType> implements FileAndTypeService {
    @Override
    public List<String> getTypeIdByFileId(String fileId) {
        List<FileAndType> fileAndTypeList = query().eq("fid", fileId).list();
        if (fileAndTypeList == null || fileAndTypeList.isEmpty()) {
            return null;
        }
        return fileAndTypeList.stream().map(fileAndType -> fileAndType.getTid()).collect(Collectors.toList());
    }

    @Override
    public List<String> getFileIdByTypeId(String typeId) {
        List<FileAndType> fileAndTypeList = query().eq("tid", typeId).list();
        if (fileAndTypeList == null || fileAndTypeList.isEmpty()) {
            return null;
        }
        return fileAndTypeList.stream().map(fileAndType -> fileAndType.getFid()).collect(Collectors.toList());
    }

    @Override
    public boolean deleteByFileId(String fileId) {
        if (StrUtil.hasEmpty(fileId)) {
            return false;
        }
        boolean flag = remove(new QueryWrapper<FileAndType>().eq("fid", fileId));
        return flag;
    }

    @Override
    public boolean existenceFileAndTypeByTypeId(String typeId) {
        return query().eq("tid", typeId).count() > 0;
    }
}
